import Vue from 'vue'
import Router from 'vue-router'
import index from '@/components/index'
import header from '@/components/header'
import login from '@/components/login'
import shebeiDT from '@/components/shebeiDT'
import shebeiZT from '@/components/shebeiZT'
import shujuJC from '@/components/shujuJC'
import cheliangGL from '@/components/cheliangGL'
import sxtGL from '@/components/sxtGL'
import tongdaoCK from '@/components/tongdaoCK'
import zhujiGL from '@/components/zhujiGL'
import sxtZT from '@/components/sxtZT'
import xmGL from '@/components/xmGL'
import ycSJ from '@/components/ycSJ'
import ycPZ from '@/components/yuanchengPZ'
import shebeiJZ from '@/components/shebeiJZ'

Vue.use(Router)
Vue.component("Header",header)
const router = new Router({
  routes: [
    {
      path: '/',
      name: 'login',
      component: login,
    },
    {
      path: '/index',
      name: 'index',
      component: index,
      redirect:'/index',
      children:[
        {
          path:'/shebeiDT',
          name:'shebeiDT',
          component:shebeiDT
        },
         {
          path:'/shebeiJZ',
          name:'shebeiJZ',
          component:shebeiJZ
        },
        {
          path:'/shebeiZT',
          name:'shebeiZT',
          component:shebeiZT
        },
        {
          path:'/shujuJC',
          name:'shujuJC',
          component:shujuJC
        },
        {
          path:'/cheliangGL',
          name:'cheliangGL',
          component:cheliangGL
        },
        {
          path:'/sxtGL',
          name:'sxtGL',
          component:sxtGL
        },
        {
          path:'/tongdaoCK',
          name:'tongdaoCK',
          component:tongdaoCK
        },
        {
          path:'/zhujiGL',
          name:'zhujiGL',
          component:zhujiGL
        },
        {
          path:'/sxtZT',
          name:'sxtZT',
          component:sxtZT
        },
        {
          path:'/xmGL',
          name:'xmGL',
          component:xmGL
        },
        {
          path:'/ycSJ',
          name:'ycSJ',
          component:ycSJ
        },
        {
          path:'/ycPZ',
          name:'ycPZ',
          component:ycPZ
         },
    
      ]
    },
    
  ]
})
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
router.beforeEach((to, from, next) => {

  // console.log(to)
  // 如果路由地址是login  放行
  if (to.path === '/'){
    return next()
  } 

  // localStorage
  const token = localStorage.token
  // 如果没有token  代表没有登录 强制跳转到登录页面
  if (!token) return next('/')

  //  如果登录过期 
  let EXPIRESTIME = 24*60*60*1000;
  let date = (new Date()).getTime();
  if( localStorage.startTime === undefined || date-localStorage.startTime > EXPIRESTIME){
    return next('/');
  }
  localStorage.startTime = (new Date()).getTime();
  // 放行

  next()
  
})

export default router